package com.xiaoyun.xpath;

import com.xiaoyun.xpath.handler.annotation.Xpath;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

/**
 * 测试用例
 */
public class TestXpath {

    public static void main(String[] args) {
        //获得html页面 的body
        String body = getBody("7d784877e017c6a51nVy0ti6GVRQ.html");
        //创建 xpath 解析器
        XpathHandler xpathHandler = new XpathHandlerStrategy();
        // 可以使用 @xpath 注解放在类上,可以自动解析
        BossItem bossItem1 = new BossItem();
        bossItem1.setId("1");
        BossItem bossItem = xpathHandler.handler(body, bossItem1);

        System.out.println("1: "+ bossItem);
        System.out.println("2: "+ bossItem1);
        //或者手动 添加属性 的map 映射 key为 实体类的属性 value为xpath表达式
        //当map与 实体类的属性的注解 都有映射时, 优先 使用map里的 映射
        //Map<String, String> map = new HashMap<>();
        //map.put("jobTitle", "//div[@class='info-primary']/div[@class='name']/h1/text()");
        //BossItem bossItem = xpathHandler.handler(body, BossItem.class, map);
//        System.out.println(bossItem);
    }

    private static String getBody(String url) {
        String userDir = System.getProperty("user.dir") + File.separator + url;
        File file = new File(userDir);
        StringBuilder bodyBuilder = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buf = new byte[1024]; //数据中转站 临时缓冲区
            int length = 0;
            //循环读取文件内容，输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
            //当文件读取到结尾时返回 -1,循环结束。
            while ((length = fis.read(buf)) != -1) {
                bodyBuilder.append(new String(buf, 0, length));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bodyBuilder.toString();
    }
}

class BossItem {
    private String id;
    private String url;
    @Xpath(pattern = "//div[@class='info-primary']/div[@class='name']/h1/text()")
    private String jobTitle;
    @Xpath(pattern = "//div[@class='job-sec']/div[@class='text']/text()")
    private String jobDescribe;
    @Xpath(pattern = "//div[@class='location-address']/text()")
    private String address;
    @Xpath(pattern = "//div[@class='name']/span[@class='salary']/text()")
    private String price;
    @Xpath(pattern = "//div[@class='job-sec']/div[@class='name']/text()")
    private String company;
    @Xpath(pattern = "//div[@class='location-address']/text();//div[@class='job-sec']/div[@class='text']/text()")
    private Map<String, String> map;

    @Override
    public String toString() {
        return "BossItem{" +
                "id='" + id + '\'' +
                ", url='" + url + '\'' +
                ", jobTitle='" + jobTitle + '\'' +
                ", jobDescribe='" + jobDescribe + '\'' +
                ", address='" + address + '\'' +
                ", price='" + price + '\'' +
                ", company='" + company + '\'' +
                ", map=" + map +
                '}';
    }

    public BossItem(String id, String url, String jobTitle, String jobDescribe, String address, String price, String company, Map<String, String> map) {
        this.id = id;
        this.url = url;
        this.jobTitle = jobTitle;
        this.jobDescribe = jobDescribe;
        this.address = address;
        this.price = price;
        this.company = company;
        this.map = map;
    }

    public BossItem() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getJobDescribe() {
        return jobDescribe;
    }

    public void setJobDescribe(String jobDescribe) {
        this.jobDescribe = jobDescribe;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

